/*
 * DTOShipStatus.cpp
 *
 *  Created on: Nov 8, 2011
 *      Author: gonzalo
 */

#include "DTOShipStatus.h"
#include "DTOEquipment.h"
#include "Constantes.h"

DTOShipStatus::DTOShipStatus(std::string& aDeserealizar,std::string owner) : DTO(SHIP_STATUS,owner){
	deserializar(aDeserealizar);
}

DTOShipStatus::DTOShipStatus(std::string owner,std::string imagen,
		float velRotacion,int velMaxima,int propulsion,int friccion,
		int duracionBala,int angle,int posX,int posY,std::list<DTOEquipment*> userEquipment,
		bool drawable,int lives, int basePorcent) : DTO(SHIP_STATUS,owner){

	this->imagen = imagen;
	this->velRotacion = velRotacion;
	this->velMaxima = velMaxima;
	this->propulsion = propulsion;
	this->friccion = friccion;
	this->duracionBala = duracionBala;
	this->angle = angle;
	this->drawable = drawable;

	this->posX = posX;
	this->posY = posY;

	this->lives = lives;
	this->basePorcent = basePorcent;

	this->DTOuserEquipment = userEquipment;
}

std::string DTOShipStatus::serializar(){
	std::stringstream retorno;

	retorno << UtilString::TOlenghtStringStringValue(posX);
	retorno << UtilString::TOlenghtStringStringValue(posY);
	retorno << UtilString::TOlenghtStringStringValue(velRotacion);
	retorno << UtilString::TOlenghtStringStringValue(velMaxima);
	retorno << UtilString::TOlenghtStringStringValue(propulsion);
	retorno << UtilString::TOlenghtStringStringValue(friccion);
	retorno << UtilString::TOlenghtStringStringValue(duracionBala);
	retorno << UtilString::TOlenghtStringStringValue(angle);
	retorno << UtilString::TOlenghtStringStringValue(imagen);
	retorno << UtilString::TOlenghtStringStringValue(drawable);
	retorno << UtilString::TOlenghtStringStringValue(lives);
	retorno << UtilString::TOlenghtStringStringValue(basePorcent);

	//serializo userEquipment
	std::list<DTOEquipment*>::iterator it;
	for (it = DTOuserEquipment.begin(); it != DTOuserEquipment.end(); ++it) {
		retorno << (*it)->serializar();
	}

	return retorno.str();
}

void DTOShipStatus::deserializar(std::string& string){
	posX = UtilString::stringToInt(UtilString::FROMlenghtStringStringValue(string));
	posY = UtilString::stringToInt(UtilString::FROMlenghtStringStringValue(string));
	velRotacion = UtilString::stringToFloat(UtilString::FROMlenghtStringStringValue(string));
	velMaxima = UtilString::stringToInt(UtilString::FROMlenghtStringStringValue(string));
	propulsion = UtilString::stringToInt(UtilString::FROMlenghtStringStringValue(string));
	friccion = UtilString::stringToInt(UtilString::FROMlenghtStringStringValue(string));
	duracionBala = UtilString::stringToInt(UtilString::FROMlenghtStringStringValue(string));
	angle = UtilString::stringToInt(UtilString::FROMlenghtStringStringValue(string));
	imagen = UtilString::FROMlenghtStringStringValue(string);
	drawable = UtilString::stringToBool(UtilString::FROMlenghtStringStringValue(string));
	lives = UtilString::stringToInt(UtilString::FROMlenghtStringStringValue(string));
	basePorcent = UtilString::stringToInt(UtilString::FROMlenghtStringStringValue(string));

	//deserializo userEquipment
	while(!string.empty()){
		DTOEquipment* dtoEquipment = new DTOEquipment(string,getOwner());
		DTOuserEquipment.push_back(dtoEquipment);
	}

}

DTOShipStatus::~DTOShipStatus(){
	std::list<DTOEquipment*>::iterator it = DTOuserEquipment.begin();
	while (it != DTOuserEquipment.end()){
		//Borra de memoria
		delete *it;

		//remover
		it = DTOuserEquipment.erase(it);
	}
}
